package com.elinkway.infinitemovies.view;

import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.CloudDiskBean;
import com.le123.ysdq.R;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by fuliqiang on 2017/4/21.
 */

public class DefinitionManager {
    public static class DefinitionObj {
        public String defType;//客户端清晰度标识,标清,高清等
        public String bitStream;//码流,云盘使用video_case,乐视源使用pls
        public int priority;//优先级0-n
        public int showTextResId;//用于界面显示的文字
        public String defObjType;//本对象所属的视频源,云盘,LETV,外网等....

        public DefinitionObj(String defType, String bitStream
                , int priority, int showTextResId, String defObjType) {
            this.bitStream = bitStream;
            this.defType = defType;
            this.priority = priority;
            this.showTextResId = showTextResId;
            this.defObjType = defObjType;
        }
    }

    public static final String DEFINITION_TYPE_SUPER = "SUPER";
    public static final String DEFINITION_TYPE_HIGH = "HIGH";
    public static final String DEFINITION_TYPE_NORMAL = "NORMAL";
    public static final String DEFINITION_TYPE_FLUENCY = "FLUENCY";
    //LETV PLS 码流
    public static final String DEFINITION_OBJ_TYPE_LETV = "LETV";
    /**
     * 流畅
     */
    public static final String PLS_MP4 = "252009";
    public static final String PLS_MP4_350 = "252021";
    /**
     * 标清
     */
    public static final String PLS_MP4_800 = "252013";
    /**
     * 高清
     */
    public static final String PLS_MP4_720p_db = "252022";
    /**
     * 超清
     */
    public static final String PLS_MP4_720P = "252051";
    public static final String PLS_FLV_720P = "252018";
    public static final String PLS_FLV_720P_3D = "252029";

    //云盘
    public static final String DEFINITION_OBJ_TYPE_CLOUD = "CLOUD";
    public static final String CLOUD_DISK_VIDEO_TYPE_1 = "video_1";//标清--客户端标签名:流畅
    public static final String CLOUD_DISK_VIDEO_TYPE_2 = "video_2";//超清--客户端标签名:标清
    public static final String CLOUD_DISK_VIDEO_TYPE_3 = "video_3";//高清--客户端标签名:高清
    public static final String CLOUD_DISK_VIDEO_TYPE_4 = "video_4";//原画--客户端标签名:超清
    public static final String CLOUD_DISK_VIDEO_TYPE_5 = "video_5";//720P--客户端标签名:高清
    public static final String CLOUD_DISK_VIDEO_TYPE_6 = "video_6";//1080P--客户端标签名:超清

    //外网
    public static final String DEFINITION_OBJ_TYPE_OUTER = "OUTER";
    public static final String OUTER_DEF_FORMAT_0 = "0";//对应format标签-normal,客户端显示-流畅
    public static final String OUTER_DEF_FORMAT_1 = "1";//对应format标签-high,客户端显示-标清
    public static final String OUTER_DEF_FORMAT_2 = "2";//对应format标签-super,客户端显示-高清
    public static final String OUTER_DEF_FORMAT_3 = "3";//对应format标签-super,客户端显示-超清
    public static final String OUTER_DEF_FORMAT_4 = "4";//对应format标签-normal,客户端显示-原画

    public static HashMap<String, DefinitionObj> allDefMap = new HashMap<>();
    public static HashMap<String, DefinitionObj> cloudDefMap = new HashMap<>();
    public static HashMap<String, DefinitionObj> letvDefMap = new HashMap<>();
    public static HashMap<String, DefinitionObj> outerNetDefMap = new HashMap<>();

    private static void buildOuterDefinition() {
        outerNetDefMap.clear();

        outerNetDefMap.put(OUTER_DEF_FORMAT_0
                , new DefinitionObj(DEFINITION_TYPE_FLUENCY
                        , OUTER_DEF_FORMAT_0, 1
                        , R.string.definition_fluency
                        , DEFINITION_OBJ_TYPE_OUTER));
        outerNetDefMap.put(OUTER_DEF_FORMAT_1
                , new DefinitionObj(DEFINITION_TYPE_NORMAL
                        , OUTER_DEF_FORMAT_1, 0
                        , R.string.definition_common
                        , DEFINITION_OBJ_TYPE_OUTER));
        outerNetDefMap.put(OUTER_DEF_FORMAT_2
                , new DefinitionObj(DEFINITION_TYPE_HIGH
                        , OUTER_DEF_FORMAT_2, 2
                        , R.string.definition_highclear
                        , DEFINITION_OBJ_TYPE_OUTER));
        outerNetDefMap.put(OUTER_DEF_FORMAT_3
                , new DefinitionObj(DEFINITION_TYPE_SUPER
                        , OUTER_DEF_FORMAT_3, 3
                        , R.string.definition_super2
                        , DEFINITION_OBJ_TYPE_OUTER));
    }

    private static void buildLetvDefinition() {
        letvDefMap.clear();
        letvDefMap.put(PLS_MP4
                , new DefinitionObj(DEFINITION_TYPE_FLUENCY
                        , PLS_MP4, 1
                        , R.string.definition_fluency
                        , DEFINITION_OBJ_TYPE_LETV));
        letvDefMap.put(PLS_MP4_350
                , new DefinitionObj(DEFINITION_TYPE_FLUENCY
                        , PLS_MP4_350, 2
                        , R.string.definition_fluency
                        , DEFINITION_OBJ_TYPE_LETV));
        letvDefMap.put(PLS_MP4_800
                , new DefinitionObj(DEFINITION_TYPE_NORMAL
                        , PLS_MP4_800, 0
                        , R.string.definition_common
                        , DEFINITION_OBJ_TYPE_LETV));
        letvDefMap.put(PLS_MP4_720p_db
                , new DefinitionObj(DEFINITION_TYPE_HIGH
                        , PLS_MP4_720p_db, 0
                        , R.string.definition_highclear
                        , DEFINITION_OBJ_TYPE_LETV));
        letvDefMap.put(PLS_MP4_720P
                , new DefinitionObj(DEFINITION_TYPE_SUPER
                        , PLS_MP4_720P, 0
                        , R.string.definition_super2
                        , DEFINITION_OBJ_TYPE_LETV));
        letvDefMap.put(PLS_FLV_720P
                , new DefinitionObj(DEFINITION_TYPE_SUPER
                        , PLS_FLV_720P, 0
                        , R.string.definition_super2
                        , DEFINITION_OBJ_TYPE_LETV));
        letvDefMap.put(PLS_FLV_720P_3D
                , new DefinitionObj(DEFINITION_TYPE_SUPER
                        , PLS_FLV_720P_3D, 0
                        , R.string.definition_super2
                        , DEFINITION_OBJ_TYPE_LETV));
    }

    private static void buildCloudDefinition() {
        cloudDefMap.clear();
        cloudDefMap.put(CLOUD_DISK_VIDEO_TYPE_1
                , new DefinitionObj(DEFINITION_TYPE_FLUENCY
                        , CLOUD_DISK_VIDEO_TYPE_1, 0
                        , R.string.definition_fluency
                        , DEFINITION_OBJ_TYPE_CLOUD));
        cloudDefMap.put(CLOUD_DISK_VIDEO_TYPE_2
                , new DefinitionObj(DEFINITION_TYPE_NORMAL
                        , CLOUD_DISK_VIDEO_TYPE_2, 1
                        , R.string.definition_common
                        , DEFINITION_OBJ_TYPE_CLOUD));
        cloudDefMap.put(CLOUD_DISK_VIDEO_TYPE_3
                , new DefinitionObj(DEFINITION_TYPE_HIGH
                        , CLOUD_DISK_VIDEO_TYPE_3, 2
                        , R.string.definition_highclear
                        , DEFINITION_OBJ_TYPE_CLOUD));
        cloudDefMap.put(CLOUD_DISK_VIDEO_TYPE_4
                , new DefinitionObj(DEFINITION_TYPE_SUPER
                        , CLOUD_DISK_VIDEO_TYPE_4, 5
                        , R.string.definition_super2
                        , DEFINITION_OBJ_TYPE_CLOUD));
        cloudDefMap.put(CLOUD_DISK_VIDEO_TYPE_5
                , new DefinitionObj(DEFINITION_TYPE_HIGH
                        , CLOUD_DISK_VIDEO_TYPE_5, 3
                        , R.string.definition_highclear
                        , DEFINITION_OBJ_TYPE_CLOUD));
        cloudDefMap.put(CLOUD_DISK_VIDEO_TYPE_6
                , new DefinitionObj(DEFINITION_TYPE_SUPER
                        , CLOUD_DISK_VIDEO_TYPE_6, 4
                        , R.string.definition_super2
                        , DEFINITION_OBJ_TYPE_CLOUD));
    }

    //构建全部清晰度对象, 包括乐视源,云盘源和外网源
    private static void buildDefinition() {
        allDefMap.clear();
        buildCloudDefinition();
        buildLetvDefinition();
        buildOuterDefinition();
        allDefMap.putAll(cloudDefMap);
        allDefMap.putAll(letvDefMap);
        allDefMap.putAll(outerNetDefMap);
    }

    //判断清晰度对象是否为云盘对象
    public static boolean isCloudDefObj(DefinitionObj definitionObj) {
        return TextUtils.equals(definitionObj.defObjType, DEFINITION_OBJ_TYPE_CLOUD);
    }

    //判断清晰度对象是否为乐视源对象
    public static boolean isLetvDefObj(DefinitionObj definitionObj) {
        return TextUtils.equals(definitionObj.defObjType, DEFINITION_OBJ_TYPE_LETV);
    }

    public static boolean checkDefTypeInLetv(String typeFlag) {
        buildLetvDefinition();
        return !TextUtils.isEmpty(typeFlag) && letvDefMap.containsKey(typeFlag);
    }

    public static boolean checkDefTypeInCloud(String typeFlag) {
        buildCloudDefinition();
        return !TextUtils.isEmpty(typeFlag) && cloudDefMap.containsKey(typeFlag);
    }

    public static boolean checkDefTypeInOuter(String typeFlag) {
        buildOuterDefinition();
        return !TextUtils.isEmpty(typeFlag) && outerNetDefMap.containsKey(typeFlag);
    }

    /**
     * 根据videocase list返回是否支持各清晰度, 不区分源
     *
     * @param typeList 传入码流名称列表, video_2,  252051等
     * @return 各清晰度的支持boolean值, 不支持也会返回
     */
    public static HashMap<String, Boolean> getSupportDefFlag(List<String> typeList) {
        HashMap<String, Boolean> retMap = new HashMap<>();
        retMap.put(DEFINITION_TYPE_SUPER, false);
        retMap.put(DEFINITION_TYPE_HIGH, false);
        retMap.put(DEFINITION_TYPE_NORMAL, false);
        retMap.put(DEFINITION_TYPE_FLUENCY, false);

        ArrayList<DefinitionObj> defObjList = getSupportDefList(typeList);
        for (DefinitionObj definitionObj : defObjList) {
            retMap.put(definitionObj.defType, true);
        }
        return retMap;
    }

    /**
     * 根据videocase反查客户端对应的清晰度对象
     * 通过bitstream判断,不区分源
     *
     * @param type 传入码流名称, video_2,  252051等
     * @return 该码流所属的清晰度对象
     */
    public static DefinitionObj checkDefClassify(String type) {
        if (allDefMap == null || allDefMap.size() < 1) {
            buildDefinition();
        }
        if (TextUtils.isEmpty(type) || !allDefMap.containsKey(type)) {
            return null;
        }
        return allDefMap.get(type);
    }

    /**
     * 根据videocase列表返回对应的清晰度列表,不区分源
     *
     * @param typeList 传入码流名称列表, video_2,  252051等
     * @return 该码流所属的清晰度对象列表
     */
    public static ArrayList<DefinitionObj> getSupportDefList(List<String> typeList) {
        if (allDefMap == null || allDefMap.size() < 1) {
            buildDefinition();
        }
        ArrayList<DefinitionObj> retList = new ArrayList<>();
        if (typeList != null && typeList.size() > 0) {
            for (String type : typeList) {
                if (allDefMap.containsKey(type)) {
                    retList.add(allDefMap.get(type));
                }
            }
        }
        return retList;
    }

    /**
     * 根据客户端清晰度值返回所支持的videocase列表,不区分源
     *
     * @param defFlag 传入清晰度标识 super, high等
     * @return 返回该清晰度下支持的所有码流标识 video_2, 252051等
     */
    public static ArrayList<String> getVideoListByDefType(String defFlag) {
        ArrayList<String> retList = new ArrayList<>();
        if (TextUtils.isEmpty(defFlag)) {
            return retList;
        }
        if (allDefMap == null || allDefMap.size() < 1) {
            buildDefinition();
        }
        Iterator iter = allDefMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, DefinitionObj> entry = (Map.Entry) iter.next();
            DefinitionObj value = entry.getValue();
            if (TextUtils.equals(value.defType, defFlag)) {
                retList.add(entry.getKey());
            }
        }
        return retList;
    }

    /**
     * 根据客户端清晰度值返回所支持的清晰度对像列表
     *
     * @param defFlag 传入清晰度标识 super, high等
     * @return 返回该清晰度对象, 同一个清晰度分类下可能会有多个对象, 每个码流一个对象
     */
    public static ArrayList<DefinitionObj> getDefObjListByDefType(String defFlag) {
        ArrayList<DefinitionObj> retList = new ArrayList<>();
        if (TextUtils.isEmpty(defFlag)) {
            return retList;
        }
//        if (cloudDefMap == null || cloudDefMap.size() < 1) {
        buildDefinition();
//        }
        Iterator iter = allDefMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, DefinitionObj> entry = (Map.Entry) iter.next();
            DefinitionObj value = entry.getValue();
            if (TextUtils.equals(value.defType, defFlag)) {
                retList.add(value);
            }
        }
        return retList;
    }

    /**
     * 根据客户端清晰度值返回一个支持的videocase,如果不包括返回默认值,不区分源
     * 用于查找某视频在某清晰度分类下的一个码流值
     * 一般传该视频所支持的所有码流列表和一个清晰度标识
     *
     * @param videoCaseList    传入码流名称列表, video_2,  252051等,
     * @param defFlag          传入清晰度标识 super, high等
     * @param defaultVideoCase 默认的码流值, 如果无法找到匹配的码流值,返回该默认值
     * @return
     */
    public static String getVideoCaseByDefType(List<String> videoCaseList
            , String defFlag, String defaultVideoCase) {
        String retString = defaultVideoCase;

        if (videoCaseList == null || videoCaseList.size() < 1) {
            return retString;
        }

        //获得清晰度值支持的videocase列表
        ArrayList<DefinitionObj> supportVideoList = getDefObjListByDefType(defFlag);
        if (supportVideoList == null || supportVideoList.size() < 1) {
            return retString;
        }
        ArrayList<DefinitionObj> tmpList = new ArrayList<>();
        //与videocase列表比对,取重复值
        for (DefinitionObj supportVideo : supportVideoList) {
            for (String hasVideo : videoCaseList) {
                if (TextUtils.equals(supportVideo.bitStream, hasVideo)) {
                    tmpList.add(supportVideo);
                }
            }
        }
        if (tmpList.size() > 0) {
            DefinitionObj definitionObj = tmpList.get(0);
            retString = definitionObj.bitStream;
        } else {
            retString = defaultVideoCase;
        }

        return retString;
    }

    /**
     * 根据云盘源返回的数据, 判断各种清晰度是否被支持
     * 仅限云盘源使用
     *
     * @param cloudDiskBean 云盘源的数据集
     * @return 返回 |super, true| high, false| 形式的map对象
     */
    public static HashMap<String, Boolean> getSupportDefMapOnlyCloud(CloudDiskBean cloudDiskBean) {
        HashMap<String, CloudDiskBean.VideoListObj> supportVideoListObj = cloudDiskBean.getSupportVideoListObj();
        ArrayList<String> videoCaseTypeList = new ArrayList();
        videoCaseTypeList.addAll(supportVideoListObj.keySet());
        return getSupportDefFlag(videoCaseTypeList);
    }

    /**
     * 根据外网支持的清晰度列表, 判断各种清晰度是否被支持
     * 仅限外网源使用
     *
     * @param typeList 外网支持的清晰度列表
     * @return 返回 |super, true| high, false| 形式的map对象
     */
    public static HashMap<String, Boolean> getSupportDefMapOnlyOuter(ArrayList<Integer> typeList) {
        ArrayList<String> stringArrayList = new ArrayList<>();
        if (typeList != null && typeList.size() > 0) {
            for (Integer i : typeList) {
                stringArrayList.add(i + "");
            }
        }
        return getSupportDefFlag(stringArrayList);
    }

    /**
     * 根据码流判断是否属于超清分类
     *
     * @param typeFlag 传入码流名称, video_2,  252051等
     * @return
     */
    public static boolean isDefinitionSuper(String typeFlag) {
        DefinitionObj definitionObj = checkDefClassify(typeFlag);
        return definitionObj != null
                && TextUtils.equals(DEFINITION_TYPE_SUPER, definitionObj.defType);
    }

    /**
     * 根据码流判断是否属于高清分类
     *
     * @param typeFlag 传入码流名称, video_2,  252051等
     * @return
     */
    public static boolean isDefinitionHigh(String typeFlag) {
        DefinitionObj definitionObj = checkDefClassify(typeFlag);
        return definitionObj != null
                && TextUtils.equals(DEFINITION_TYPE_HIGH, definitionObj.defType);
    }

    /**
     * 根据码流判断是否属于标清分类
     *
     * @param typeFlag 传入码流名称, video_2,  252051等
     * @return
     */
    public static boolean isDefinitionNormal(String typeFlag) {
        DefinitionObj definitionObj = checkDefClassify(typeFlag);
        return definitionObj != null
                && TextUtils.equals(DEFINITION_TYPE_NORMAL, definitionObj.defType);
    }

    /**
     * 根据码流判断是否属于流畅分类
     *
     * @param typeFlag 传入码流名称, video_2,  252051等
     * @return
     */
    public static boolean isDefinitionFluency(String typeFlag) {
        DefinitionObj definitionObj = checkDefClassify(typeFlag);
        return definitionObj != null
                && TextUtils.equals(DEFINITION_TYPE_FLUENCY, definitionObj.defType);
    }

    public class SortDefinition implements Comparator {

        @Override
        public int compare(Object o, Object t1) {
            return 0;
        }

        @Override
        public boolean equals(Object o) {
            return false;
        }
    }
    public static HashMap<String, String> letvVtypeDefMap = new HashMap<>();
    public static final String VIDEO_MP4 = "9";
    public static final String VIDEO_MP4_350 = "21";
    public static final String VIDEO_MP4_800 = "13";
    public static final String VIDEO_MP4_720_db = "22";
    public static final String VIDEO_MP4_720P = "51";
    public static final String VIDEO_FLV_720P = "18";
    public static final String VIDEO_FLV_720P_3D = "29";

    private static void buildLetvVtypeDefMap() {
        letvVtypeDefMap.clear();
        letvVtypeDefMap.put(PLS_MP4, VIDEO_MP4);
        letvVtypeDefMap.put(PLS_MP4_350, VIDEO_MP4_350);
        letvVtypeDefMap.put(PLS_MP4_800, VIDEO_MP4_800);
        letvVtypeDefMap.put(PLS_MP4_720p_db, VIDEO_MP4_720_db);
        letvVtypeDefMap.put(PLS_MP4_720P, VIDEO_MP4_720P);
        letvVtypeDefMap.put(PLS_FLV_720P, VIDEO_FLV_720P);
        letvVtypeDefMap.put(PLS_FLV_720P_3D, VIDEO_FLV_720P_3D);
    }
    public static String getLetvVtype(String type) {
        if (letvVtypeDefMap == null || letvVtypeDefMap.size() < 1) {
            buildLetvVtypeDefMap();
        }
        if (TextUtils.isEmpty(type) || !letvVtypeDefMap.containsKey(type)) {
            return null;
        }
        return letvVtypeDefMap.get(type);
    }
}
